package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.MaterialLocationDetailActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.MaterierPositionOrderInfo;
import com.supply.dl.bean.request.PickOrderListReq;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 领料定库列表 搜索页 modle
 * <pre>
 * 1.所有的搜索都是跳转 <linked {@link com.supply.dl.activity.SearchActivity},跳转方式如下
 * Bundle bundle = new Bundle();
 * bundle.putString("modleName", PickBillSureWareHouseModle.class.getName());
 * jumpActivity(SearchActivity.class,bundle);
 *
 * 2.新建一个搜索页，是实现接口 ISearchModle 跳转去SearchActivity达到，不应再新建新的activty
 *
 * 3.实现接口<linked {@link com.supply.dl.activity.seachmode.ISearchModle} 需要注意
 *  a.setSearchAdapter方法注定了 列表的样式数据，和点击事件，全部在这里进行自定义
 *  b.vistData方法 内部应该是进行访问获取数据，最后传递的参数，是对加载失败布局显示与否的说明。
 *
 * </pre>
 * <p>
 * Author: tian <br>
 * Time: 2018/4/28 15:09
 */
public class PickBillSureWareHouseModle extends ISearchModle<MaterierPositionOrderInfo> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;

        resource = new ArrayList<>();

        adapter = new NormalAdapter<MaterierPositionOrderInfo>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, MaterierPositionOrderInfo data) {

                holder.setText(R.id.name_tv, data.getBussTypeName());
                holder.setText(R.id.status_tv, data.getPositionStatusName());
                holder.setText(R.id.order_nub_tv, "生产订单号:" + data.getErpOrderNo());
                holder.setText(R.id.erp_nub_tv, "生产车间:" + data.getProductWorkshopName());

                holder.setText(R.id.time_tv, StringUtils.isEmpty(String.valueOf(data.getAddTime())) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.getAddTime()), DateUtils.DATE_TIME_FORMAT));//建单时间
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_obtainmaterial;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<MaterierPositionOrderInfo>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, MaterierPositionOrderInfo data, int position) {
                BaseActivity ac = (BaseActivity) context;
                Bundle bundle = new Bundle();
                bundle.putSerializable("noticeOrderId", data.getNoticeOrderId());
                ac.jumpActivity(MaterialLocationDetailActivity.class, bundle);
            }
        });

    }

    @Override
    public void setHintText() {
        hintText = "领料单号，生产订单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "领料定库";
    }

    @Override
    public void init(Context context) {
        this.context = context;

        setTitleShow();
        setSearchAdapter();
    }

    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {

        PickOrderListReq req = new PickOrderListReq();
        req.searchParam = keyWord;

        req.companyId = filterMap.get(AppConfig.SearchFilterAction.COMPANY);
        req.factoryId = filterMap.get(AppConfig.SearchFilterAction.FACTORY);
        req.productWorkshopId = filterMap.get(AppConfig.SearchFilterAction.WORKSHOP);
        req.createTimeStart = filterMap.get(AppConfig.SearchFilterAction.START_TIME);
        req.createTimeEnd = filterMap.get(AppConfig.SearchFilterAction.END_TIME);

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.getMaterierPositionList(req, String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<MaterierPositionOrderInfo>>>compose(context))
                .subscribe(new BaseObserver<List<MaterierPositionOrderInfo>>(context) {
                    @Override
                    public void onSuccess(BaseInfo<List<MaterierPositionOrderInfo>> baseInfo) {

                        if (baseInfo != null && baseInfo.getData() != null) {
                            if(pageNum == 1){
                                resource.clear();
                            }
                            if(baseInfo.getData().size() < 10){
                                hasMoreData = false;
                            }
                            resource.addAll(baseInfo.getData());
                            adapter.notifyDataSetChanged();
                        }
                        resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        resultListener.onResult();
                    }
                });

    }

}
